package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;


import android.content.Context;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.FileResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.UserInfo;

import java.io.File;

import rx.Subscriber;

/**
 * 修改用户信息fragment
 */
public class UserInfoChangedPresenter implements UserInfoContract.ChangeInfoPresenter{
    private UserInfoContract.ChangeInfoView view;

    public UserInfoChangedPresenter(UserInfoContract.ChangeInfoView view) {
        this.view = view;
        view.setPresenter(this);
    }

    @Override
    public void changeUserInfo(final UserInfo userInfo) {
        final Context context = GlobalContext.getInstance();
        if (userInfo==null) {
            view.changeInfoError(context.getString(R.string.tips_userinfo_no_set));
            return;
        }
        final String head_id = userInfo.getHead_id();
        if (StringUtils.isEmpty(head_id)) {
            view.changeInfoError(context.getString(R.string.tips_header_no_set));
            return;
        }
        if (StringUtils.isEmpty(userInfo.getNick_name())) {
            view.changeInfoError(context.getString(R.string.tips_nickname_no_set));
            return;
        }
        if (StringUtils.isEmpty(userInfo.getSex())) {
            view.changeInfoError(context.getString(R.string.tips_sex_no_set));
            return;
        }
        if (StringUtils.isEmpty(userInfo.getAge())) {
            view.changeInfoError(context.getString(R.string.tips_age_no_set));
            return;
        }
        if (new File(head_id).exists()) {
            ApiService.upload(head_id).subscribe(new Subscriber<FileResultBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    view.changeInfoError("头像上传失败，请稍后再试！");
                }

                @Override
                public void onNext(FileResultBean fileResultBean) {
                    if ("0".equals(fileResultBean.retcode)) {
                        userInfo.setHead_id(fileResultBean.ret.mediaId);
                        changeUserInfo(userInfo,context);
                    }else
                        view.changeInfoError("头像上传失败，请稍后再试！");
                }
            });
        }else
            changeUserInfo(userInfo, context);
    }

    /**
     * 修改个人信息
     * @param userInfo
     * @param context
     */
    private void changeUserInfo(UserInfo userInfo, final Context context) {
        ApiService.changeUserInfo(AppConfig.getMobile(context),userInfo.getNick_name(), userInfo.getHead_id(),userInfo.getSex(),userInfo.getAge())
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.changeInfoError(context.getString(R.string.tips_userinfo_change_fail));
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (context.getString(R.string.type_custemer).equals(resultBean.rsmsg.rscode)) {
                            view.changeInfoSuccess();
                        }else{
                            view.changeInfoError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void start() {

    }
}
